package store.web.servlet;

import store.domain.Account;
import store.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    private AccountService accountService = new AccountService();
    private static final String MAIN_PAGE = "/WEB-INF/jsp/catalog/main.jsp";
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Account account = new Account();
        account.setUsername(request.getParameter("username"));
        account.setPassword(request.getParameter("password"));
        account.setFirstName(request.getParameter("firstName"));
        account.setLastName(request.getParameter("lastName"));
        account.setEmail(request.getParameter("email"));
        account.setPhone(request.getParameter("phone"));
        account.setAddress1(request.getParameter("address1"));
        account.setAddress2(request.getParameter("address2"));
        account.setCity(request.getParameter("city"));
        account.setState(request.getParameter("state"));
        account.setZip(request.getParameter("zip"));
        account.setCountry(request.getParameter("country"));
        account.setLanguagePreference(request.getParameter("languagePreference"));
        account.setFavouriteCategoryId(request.getParameter("favouriteCategoryId"));
        account.setListOption(Boolean.parseBoolean(request.getParameter("listOption")));
        account.setBannerOption(Boolean.parseBoolean(request.getParameter("bannerOption")));

        // 存储账户信息并转到主页面
        System.out.println("即将插入的用户名: " + account.getUsername() + ", 密码: " + account.getPassword());
        accountService.insertAccount(account);
        session.setAttribute("account", account);
        request.getRequestDispatcher(MAIN_PAGE).forward(request, response);
    }
}
